package studyNote;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RuntimeExecDemo {
    public static void main(String[] args) {
        try {
            // 使用ProcessBuilder替代弃用的Runtime.exec(String)
            Process p = new ProcessBuilder("node", "-v").start();
            // 使用try-with-resources自动关闭资源
            try (InputStream is = p.getInputStream();
                 BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"))) {
                String str; // 移除冗余的null初始化
                while ((str = br.readLine()) != null) {
                    System.out.println(str);
                }
            }
        } catch (IOException e) {
            // 使用更可靠的错误处理替代printStackTrace()
            System.err.println("执行命令时发生错误: " + e.getMessage());
            // 或者使用日志框架
            Logger.getLogger(RuntimeExecDemo.class.getName()).log(Level.SEVERE, "命令执行失败", e);
        }
    }
}